package com.xiaoran.springboot4.demo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;

@Deprecated
//@Configuration
public class MyDataSourceConfig {

    //@ConfigurationProperties("spring.datasource")//把我们组件中的内容和配置文件中的内容进行绑定
    //@Bean
    public DataSource dataSource() throws SQLException {
       DruidDataSource druidDataSource= new DruidDataSource();
       //加入监控功能，防火墙功能
       druidDataSource.setFilters("stat,wall");
        return druidDataSource;
    }

    ///@Bean
    //配置druid的监控页功能
    public ServletRegistrationBean servletRegistrationBean(){
        StatViewServlet statViewServlet=new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean=
                new ServletRegistrationBean<StatViewServlet>(statViewServlet,"/druid/*");
        servletRegistrationBean.addInitParameter("loginUsername","admin");
        servletRegistrationBean.addInitParameter("loginPassword","123456");
        return servletRegistrationBean;
    }

    //@Bean
    //配置webStatFilter 用于采集web-jdbc 关联监控数据
    public FilterRegistrationBean filterRegistrationBean(){
        WebStatFilter webStatFilter=new WebStatFilter();
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(webStatFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        filterRegistrationBean.addInitParameter("exclusions"/*不监控什么*/,"/druid/*,*.js,*.gif,*.css,*.jpg,*.ico");
        return  filterRegistrationBean;
    }

}
